﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

/// <summary>
/// Summary description for SQLAccess
/// </summary>
public class SQLAccess {
    protected SqlConnection _conn = null;
    protected SqlCommand _command;
    protected SQLAccess() {
        //
        // TODO: Add constructor logic here
        //
    }

    protected bool connect() {
        if (_conn == null) {
            try {
                _conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);

                _conn.Open();
                return true;
            }
            catch (Exception e) {
                return false;
            }
        }
        else
            return false;
    }

    protected void disconnect() {
        _conn.Close();
        _conn = null;
    }

    protected DataTable executeQuery(String sql) {
        DataTable table = new DataTable();
        connect();

        SqlDataAdapter adapter = new SqlDataAdapter(sql, _conn);
        adapter.Fill(table);

        disconnect();
        return table;
    }

    protected int executeUpdateQuery(String sql) {
        int res;
        connect();

        SqlCommand command = new SqlCommand(sql, _conn);
        command.CommandType = CommandType.Text;
        res = command.ExecuteNonQuery();

        disconnect();

        return res;
    }
}